<?php

/**
 * This is the model class for table "tb_cliente".
 *
 * The followings are the available columns in table 'tb_cliente':
 * @property string $codigo
 * @property string $rif
 * @property string $razonsocial
 * @property string $telefono
 * @property string $direccion
 * @property integer $tipocliente
 * @property integer $tb_estatus_idtb_estatus
 *
 * The followings are the available model relations:
 * @property TbActualizacion[] $tbActualizacions
 * @property TbEstatus $tbEstatusIdtbEstatus
 * @property TbDetalles[] $tbDetalles
 */
class TbClienteCV extends CActiveRecord
{
	public $estadoActual;
	public $tipoActual;
	public $idDetalle;
	public $cantPendiente;
	public $cantActualizado;
	public $cantFallida;
	public $totalClientes;
	public function calcularTotales(){
		$esta = TbEstatus::model()->findByAttributes(array('estatus'=>'Pendiente'));
		$this->cantPendiente = Yii::app()->db->createCommand('SELECT count(*) from tb_cliente where tb_estatus_idtb_estatus="'.$esta->idtb_estatus.'" and tipocliente=1')->queryScalar();
		$esta = TbEstatus::model()->findByAttributes(array('estatus'=>'Actualizado'));
		$this->cantActualizado = Yii::app()->db->createCommand('SELECT count(*) from tb_cliente where tb_estatus_idtb_estatus="'.$esta->idtb_estatus.'" and tipocliente=1')->queryScalar();
		$esta = TbEstatus::model()->findByAttributes(array('estatus'=>'Fallida'));
		$this->cantFallida = Yii::app()->db->createCommand('SELECT count(*) from tb_cliente where tb_estatus_idtb_estatus="'.$esta->idtb_estatus.'" and tipocliente=1')->queryScalar();
		$this->totalClientes = Yii::app()->db->createCommand('SELECT count(*) from tb_cliente where  tipocliente=1')->queryScalar();
	}
	/**
	 * Returns the static model of the specified AR class.
	 * @return TbClienteCV the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tb_cliente';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('codigo, rif, razonsocial', 'required'),
			array('tipocliente, tb_estatus_idtb_estatus', 'numerical', 'integerOnly'=>true),
			array('rif','match','pattern'=>'/[JVE]-\d+-\d/'),	
			array('codigo, rif, telefono', 'length', 'max'=>20),
			array('razonsocial', 'length', 'max'=>150),
			array('direccion', 'length', 'max'=>200),
			array('codigo','unique'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('codigo, rif, razonsocial, telefono, direccion, tipocliente, tb_estatus_idtb_estatus', 'safe', 'on'=>'search'),
		);
	}
	public function obtenerTipoCliente(){
		return array('1'=>'Corporacion CV','2'=>'Lufioca');
	}
	
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tbActualizacions' => array(self::HAS_MANY, 'TbActualizacion', 'tb_cliente_codigo'),
			'tbEstatusIdtbEstatus' => array(self::BELONGS_TO, 'TbEstatus', 'tb_estatus_idtb_estatus'),
			'tbDetalles' => array(self::HAS_MANY, 'TbDetalles', 'tb_cliente_codigo'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'codigo' => 'Codigo',
			'rif' => 'Rif',
			'razonsocial' => 'Razon Social',
			'telefono' => 'Telefono',
			'direccion' => 'Direccion',
			'tipocliente' => 'Tipo de Cliente',
			'tb_estatus_idtb_estatus' => 'Estado',
			'estadoActual'=>'Estado',
			'tipoActual'=>'Tipo de Cliente',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		
		$criteria=new CDbCriteria;

		$criteria->compare('codigo',$this->codigo,true);
		$criteria->compare('rif',$this->rif,true);
		$criteria->compare('razonsocial',$this->razonsocial,true);
		$criteria->compare('telefono',$this->telefono,true);
		$criteria->compare('direccion',$this->direccion,true);
		//$criteria->compare('tipocliente',$this->tipocliente);
		$criteria->compare('tipocliente','1');
		$criteria->compare('tb_estatus_idtb_estatus',$this->tb_estatus_idtb_estatus);
		//$criteria->condition='tipocliente=1';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function esSupervisor(){
		return (Yii::app()->user->checkAccess("Supervisor"));
	}
	protected function afterValidate(){
		parent::afterValidate();
		$this->tipocliente=1;
		
	}
	protected function afterFind(){
		parent::afterFind();
		$es = TbEstatus::model()->findByPk($this->tb_estatus_idtb_estatus);
		$this->estadoActual = $es->estatus;
		switch($this->tipocliente){
			case 1: $this->tipoActual = 'Corporacion CV';
					break;
			case 2: $this->tipoActual = 'Lufioca';
					break;
		}
		$det = TbDetalles::model()->findByAttributes(array('tb_cliente_codigo'=>$this->codigo));
		if ($det===null){
			$this->idDetalle=-1;	
		}else{
			$this->idDetalle=$det->idtb_detalles;
		}
		
	}
	
	public function obtenerEstados(){
		return (CHtml::listData(TbEstatus::model()->findAll(),'idtb_estatus','estatus'));
	}
	public function estaVisible(){
		if ($this->idDetalle==-1)
			return false;
		return true;
	}
	
}